<script type="text/javascript">

    var catagoryData    = <?php    echo json_encode($this->categories)         ?>;
    var subcatData      = <?php    echo json_encode($this->subcats)      ?>;
    var labelData       = <?php    echo json_encode($this->labels)       ?>;
    var formFieldData  = <?php    echo json_encode($this->formFieldData)   ?>;

$(document).ready(function(){
    $('#subcat_layout').hide();
    $('#label_layout').hide();
    $(".category_dropdown").change(function(){
        var cat_val = $(".category_dropdown").val();
        var options_subcat = '';
        options_subcat += '<option value="' + '' + '">' + 'Select Subcategory' + '</option>';
        $.each(subcatData,function(key,val){
            if(cat_val == this.categoryid)
            {
                options_subcat += '<option value="' + key + '">' + val.subcategoryname + '</option>';
            }
        });
        $('.sub_category_dropdown').html(options_subcat);
        $('#subcat_layout').show();
    });

    $(".sub_category_dropdown").change(function(){
        $('#label_layout').show();
        var subcat_val = $(".sub_category_dropdown").val();
        var options_label = '';
        options_label += '<option value="' + '' + '">' + 'Select Label' + '</option>';
        $.each(labelData,function(key,val){
            if(subcat_val == this.subcategoryid)
            {
                options_label += '<option value="' + key + '">' + val.labelname + '</option>';
            }
        });
        $('.label_dropdown').html(options_label);
    });
    
    $(".label_dropdown").change(function(){
        var c_id = $(".category_dropdown").val();
        var sc_id = $(".sub_category_dropdown").val();
        var l_id = $(".label_dropdown").val();
        var cnt=0;
        var asset_form_field = '';
        asset_form_field += '<br/><form name=Assetform action=asset/index/save method=POST>';
        $.each(formFieldData,function(key,val){
            if(c_id == this.catid && sc_id == this.subcatid && l_id == this.labelid)
            {
                if(this.input_type == 'textbox'){
                    asset_form_field += this.field_name + ' : ' + '<input type=text name="' + encodeURIComponent(this.field_name) + '+' + key +'">' ;
                }
                cnt++;
            }
        });
        asset_form_field += '<input type=hidden name=hidden_cat_id value="' + c_id +'">';
        asset_form_field += '<input type=hidden name=hidden_subcat_id value="' + sc_id +'">';
        asset_form_field += '<input type=hidden name=hidden_label_id value="' + l_id +'">';
        asset_form_field += '<input type=submit name=submitBtn value=Create Asset>';
        asset_form_field += '</form>';
        $('.assetForm').html(asset_form_field);
    });
});

</script>

<h1>Add Asset</h1>
<div id="cat_layout">
Category : <select class="category_dropdown">
                <?php foreach($this->categories as $catid=>$catname) {?>
                    <option value="<?=$catid?>" ><?=$catname?></option>                
                <?php }?>   
           </select> 
</div>
</br>
<div id="subcat_layout">
Category : <select class="sub_category_dropdown">
           </select>
</div>
</br>
<div id="label_layout">
Label    : <select class="label_dropdown">
           </select>
</div>

<div class="assetForm">
</div>
